import json

# 从知识图谱提取词语（确保UTF-8编码）
with open("emotion_kg.json", "r", encoding="utf-8") as f:
    emotion_data = json.load(f)
    kg_words = list(emotion_data["words"].keys())

# 生成自定义词典文件（添加词频和词性标注）
with open("custom_words1.txt", "w", encoding="utf-8") as f:
    for word in kg_words:
        # 获取词性和情感分数（如果存在）
        polarity = emotion_data["words"][word].get("polarity", "")
        score = emotion_data["words"][word].get("sentiment_score", 0)

        # 格式：词语 词频 词性 情感分数
        f.write(f"{word} 1000 n {score}\n")  # n表示名词，可根据实际情况调整

print(f"已生成包含 {len(kg_words)} 个词语的自定义词典")